package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.adapter.FamilyNameListAdapter;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.bean.ReasionInfoCallbackBean;
import com.chuanjiabao.wisdom.databinding.ActivitySelectAllocationBinding;
import com.chuanjiabao.wisdom.listener.OnPositionClickListener;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.ui.view.TaoLinear;
import com.chuanjiabao.wisdom.util.GsonUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.LogUtil;
import com.chuanjiabao.wisdom.util.SpResourceUtil;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 86178 on 2019/8/25.
 * author mogen
 * 用心，成就一切。
 */

public class SelectAllocationActivity extends BaseActivity {

    ActivitySelectAllocationBinding binding;

    ReasionInfoCallbackBean.DataBean bean;
    String homeId;

    FamilyNameListAdapter adapter;
    List<FamilyDeatilCallbackBean.DataBean.MembersBean> names = new ArrayList<>();
    List<FamilyDeatilCallbackBean.DataBean.MembersBean> namesSecond = new ArrayList<>();

    //名字
    List<String> strs = new ArrayList<>();
    List<String> strsSecond = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_select_allocation);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setBack();
        setRightText("确定");
        setRightTextDraw(R.drawable.module_draw_success_ok);

        homeId = SpResourceUtil.getHomeId();
        if (homeId != null) {
            getFamilyDetail(homeId);
        }

        adapter = new FamilyNameListAdapter(this, names);
        TaoLinear taoLinear = new TaoLinear(SelectAllocationActivity.this, 4);
        taoLinear.setScrollEnabled(true);
        binding.recycler.setLayoutManager(taoLinear);
        binding.recycler.setAdapter(adapter);

        adapter.setOnPositionClickListener(new OnPositionClickListener() {
            @Override
            public void setOnPositionClickListener(int position) {
                if (!strs.contains(names.get(position).getD_id() + "")) {
                    strs.add(names.get(position).getD_id() + "");
                } else {
                    strsSecond = strs;
                    strsSecond.remove(names.get(position).getD_id() + "");
                    strs = strsSecond;
                }

                for (String s : strs) {
                    LogUtil.e(s);
                }
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rightText:
                String relation = "";
                if (strs.size() != 0) {
                    for(String s: strs){
                        relation += "," + s;
                    }
                    relation = relation.substring(1,relation.length());
                }
                Intent intent = new Intent();
                intent.setAction("cc.android.myaction.image");
                intent.putExtra("module_bean", relation);
                setResult(RESULT_OK, intent);
                finish();
                break;
                default:
        }
    }

    public void getFamilyDetail(String homeId) {
        HTTPAPI.getInstance().getFamilyDetail(homeId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(SelectAllocationActivity.this, s);
                } else {
                    try {
                        FamilyDeatilCallbackBean callbackBean = GsonUtil.GsonToBean(s, FamilyDeatilCallbackBean.class);
                        names.clear();
                        for (FamilyDeatilCallbackBean.DataBean dataBean : callbackBean.getData()) {
                            for (FamilyDeatilCallbackBean.DataBean.MembersBean membersBean : dataBean.getMembers()) {
                                namesSecond.add(membersBean);
                            }
                        }
                        names.addAll(namesSecond);
                        adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        LogUtil.e("解析家解析有误");
                    }
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("获取家详情失败-->" + e.getMessage());
            }
        });
    }
}
